package com.yf.web.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import com.yf.ability.desensitize.filter.DesensitizeFilter;
import com.yf.openapi.interceptor.SignInterceptor;
import com.yf.web.aspect.dict.DataDictFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;


/**
 * 全局接口相关配置
 * @author van
 */
@Configuration
public class ApiConfig implements WebMvcConfigurer {

    @Autowired
    private SignInterceptor signInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 开放接口权限过滤
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/open/**").excludePathPatterns("/api/open/access");
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/open/codewood/**").excludePathPatterns("/api/open/codewood/code_page");
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/open/aiExamPaper/**").excludePathPatterns("/api/open/aiExamPaper/createPaper");
        List<String> list1=new ArrayList<>();
        list1.add("/api/openAI/qu/qu/save");
        list1.add("/api/openAI/qu/qu/saveById");
        list1.add("/api/openAI/qu/qu/delete");
        list1.add("/api/openAI/qu/qu/detail");
        list1.add("/api/openAI/qu/qu/detail-for-train");
        list1.add("/api/openAI/qu/qu/paging");
        list1.add("/api/openAI/qu/qu/paging-with-answer");
        list1.add("/api/openAI/qu/qu/list-with-chapter-group");
        list1.add("/api/openAI/qu/qu/join-chapters");
        list1.add("/api/openAI/qu/qu/clear-chapters");
        list1.add("/api/openAI/qu/qu/batch-level");
        list1.add("/api/openAI/qu/qu/batch-repo");
        list1.add("/api/openAI/qu/qu/import");
        list1.add("/api/openAI/qu/qu/export");
        list1.add("/api/openAI/qu/qu/import/template");
        List<String> list2=new ArrayList<>();
        list2.add("/api/openAI/repo/save");
        list2.add("/api/openAI/repo/saveById");
        list2.add("/api/openAI/repo/delete");
        list2.add("/api/openAI/repo/detail");
        list2.add("/api/openAI/repo/paging");
        list2.add("/api/openAI/repo/user-paging");
        list2.add("/api/openAI/repo/type-group");
        list2.add("/api/openAI/repo/chapter-group");
        List<String> list3=new ArrayList<>();
        list3.add("/api/openAI/tmpl/tmpl/save");
        list3.add("/api/openAI/tmpl/tmpl/copy");
        list3.add("/api/openAI/tmpl/tmpl/preview");
        list3.add("/api/openAI/tmpl/tmpl/delete");
        list3.add("/api/openAI/tmpl/tmpl/detail");
        list3.add("/api/openAI/tmpl/tmpl/simple-info");
        list3.add("/api/openAI/tmpl/tmpl/paging");
        list3.add("/api/openAI/tmpl/tmpl/openPaper");
        list3.add("/api/openAI/tmpl/tmpl/testId");
        List<String> list4=new ArrayList<>();
        list4.add("/api/openAI/course/course/save");
        list4.add("/api/openAI/course/course/saveById");
        list4.add("/api/openAI/course/course/delete");
        list4.add("/api/openAI/course/course/detail");
        list4.add("/api/openAI/course/course/simple-detail");
        list4.add("/api/openAI/course/course/paging");
        list4.add("/api/openAI/course/course/state");
        list4.add("/api/openAI/course/course/user-paging");
        list4.add("/api/openAI/course/course/prepare-notify");
        list4.add("/api/openAI/course/course/update-fileStatus");
        list4.add("/api/openAI/course/course/addKnowledgePoint");
        List<String> list5=new ArrayList<>();
        list5.add("/api/openAI/exam/exam/save");
        list5.add("/api/openAI/exam/exam/saveById");
        list5.add("/api/openAI/exam/exam/delete");
        list5.add("/api/openAI/exam/exam/detail");
        list5.add("/api/openAI/exam/exam/simple-detail");
        list5.add("/api/openAI/exam/exam/state");
        list5.add("/api/openAI/exam/exam/online-paging");
        list5.add("/api/openAI/exam/exam/paging");
        list5.add("/api/openAI/exam/exam/review-paging");
        list5.add("/api/openAI/exam/exam/check-info");
        list5.add("/api/openAI/exam/exam/prepare-notify");
        list5.add("/api/openAI/exam/exam/openAISaveExam");
        list5.add("/api/openAI/exam/exam/createPaper");
        List<String> list6=new ArrayList<>();
        list6.add("/api/stat/exam/class-ai");
        list6.add("/api/stat/exam/student-ai");
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/openAI/qu/qu/**").excludePathPatterns(list1);
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/openAI/repo/**").excludePathPatterns(list2);
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/openAI/tmpl/tmpl/**").excludePathPatterns(list3);
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/openAI/course/course/**").excludePathPatterns(list4);
        registry.addInterceptor(signInterceptor).addPathPatterns("/api/openAI/exam/exam/**").excludePathPatterns(list5);
//        registry.addInterceptor(signInterceptor).addPathPatterns("/api/stat/exam/**").excludePathPatterns(list6);
//        registry.addInterceptor(signInterceptor).addPathPatterns("/api/stat/exam/**").excludePathPatterns("/api/stat/exam/class-ai");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 保留原有converter,把新增fastConverter插入集合头,保证优先级
        converters.add(0, fastConverter());
    }

    /**
     * FastJson消息转换器
     *
     * @return
     */
    public static HttpMessageConverter fastConverter() {
        // 定义一个convert转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        // 添加FastJson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        // 数据脱敏及字典翻译
        fastJsonConfig.setSerializeFilters(new DesensitizeFilter(),new DataDictFilter());
        // 默认转换器
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullListAsEmpty);
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));

        // 处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        fastConverter.setSupportedMediaTypes(fastMediaTypes);
        // 在convert中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);

        return fastConverter;
    }

}
